package com.sl.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sl.mapper.UserCompanyPositionMapper;
import com.sl.pojo.UserCompanyPosition;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class UserCompanyPositionService {
    @Resource
    private UserCompanyPositionMapper userCompanyPositionMapper;

    //查询：主键
    public UserCompanyPosition findPositionById(Integer id){
        return  userCompanyPositionMapper.selectByPrimaryKey(id);
    }
    //查询：所有
    public List<UserCompanyPosition> findAllPosition(){
        return  userCompanyPositionMapper.selectAll();
    }
    //查询：UUID
    public UserCompanyPosition findByUUID(String string){
        Example example = new Example(UserCompanyPosition.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("positionId", string);
      return   userCompanyPositionMapper.selectOneByExample(example);
    }

    //增加
    public void addPosition(UserCompanyPosition userCompanyPosition){
        userCompanyPositionMapper.insertSelective(userCompanyPosition);
    }
    //修改
    public void updatePosition(UserCompanyPosition userCompanyPosition){
        userCompanyPositionMapper.updateByPrimaryKeySelective(userCompanyPosition);
    }

    public void deleteByIdPosition(UserCompanyPosition userCompanyPosition) {
        userCompanyPositionMapper.deleteByPrimaryKey(userCompanyPosition);
    }

    public Page<UserCompanyPosition> findListPotion(Map<String,String> searchMap, Integer page, Integer pageSize){
        PageHelper.startPage(page,pageSize);
        Example example = createExample(searchMap);
        List<UserCompanyPosition> userCompanyPositions = userCompanyPositionMapper.selectByExample(example);
        return (Page<UserCompanyPosition>)userCompanyPositions;
    }

    private Example createExample(Map<String, String> searchMap) {
        Example example = new Example(UserCompanyPosition.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 主键id
            if (searchMap.get("id") != null) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            if (searchMap.get("userId") != null) {
                criteria.andEqualTo("userId", searchMap.get("userId"));
            }
            if (searchMap.get("positionUuid") != null) {
                criteria.andEqualTo("positionUuid", searchMap.get("positionUuid"));
            }
            if (searchMap.get("companyId") != null) {
                criteria.andEqualTo("companyId", searchMap.get("companyId"));
            }
        }
        return example;
    }

}
